package com.itboye.textileexpres.order;

import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.MyOrderBeans;
import com.itboye.textileexpes.presenter.MyOrderPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.widget.EditTextWithDelete;

public class MyOrderShaerActivity extends BaseActivity implements
		OnClickListener, Observer, TextWatcher {
	ListView list;
	private ImageView back;
	private EditText key_word;
	private TextView search, searchCancel;
	MyOrderPresenter myOrderPresenter;
	MyOrderBeans beans;
	OrderSearchAdapter adapter;
	private RelativeLayout null_search;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_order_search;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		beans = new MyOrderBeans();
		key_word.setFocusable(true);

		key_word.setFocusableInTouchMode(true);

		key_word.requestFocus();

		key_word.requestFocusFromTouch();
		search.setVisibility(View.GONE);
		searchCancel.setVisibility(View.VISIBLE);
		myOrderPresenter = new MyOrderPresenter(this);
		key_word.addTextChangedListener(this);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MyOrderShaerActivity.this,
						ActvityOrderDetail.class);
				intent.putExtra("order_code", beans.getList().get(position)
						.getOrder_code());
				System.out.println("order_code"
						+ beans.getList().get(position).getOrder_code());
				startActivity(intent);

			}
		});

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			this.finish();
			break;
		case R.id.search:
			onSeerCh();
			break;
		case R.id.searchCancel:
			this.finish();
			break;

		default:
			break;
		}
	}

	public void onSeerCh() {

		showProgressDialog("搜索中...", true);
		if (key_word.getText().toString().trim() == null
				|| key_word.getText().toString().trim() == "") {
			toast("搜索内容不能为空！");
		} else {
			beans.setUid(IsMobileNumPhone.isUid());
			beans.setPage_index("1");
			beans.setPage_size("10");
			beans.setKeyword(key_word.getText().toString() + "");
			myOrderPresenter.update(beans);
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == myOrderPresenter.Search_success) {
				beans = (MyOrderBeans) entity.getData();

				if (beans == null || beans.getCount() <= 0) {
					null_search.setVisibility(View.VISIBLE);
					list.setVisibility(View.GONE);
				} else {
					null_search.setVisibility(View.GONE);
					list.setVisibility(View.VISIBLE);
					
				}
				adapter = new OrderSearchAdapter(beans, this);
				list.setAdapter(adapter);
				try {
					closeProgressDialog();
				} catch (Exception e) {
					// TODO: handle exception
				}

			}
		}
	}

	public class OrderSearchAdapter extends BaseAdapter {
		MyOrderBeans beans;
		Context context;

		public OrderSearchAdapter(MyOrderBeans beans, Context context) {
			// TODO Auto-generated constructor stub
			this.beans = beans;
			this.context = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return beans==null?0:beans.getList().size();// beans.getCount();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHouder houder;
			if (convertView == null) {
				houder = new ViewHouder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_orer_search, null);
				houder.name = (TextView) convertView
						.findViewById(R.id.tvOrderSearch);
				convertView.setTag(houder);
			} else {
				houder = (ViewHouder) convertView.getTag();
			}
			for (int i = 0; i < beans.getList().get(position).getItems().size(); i++) {
				houder.name.setText(beans.getList().get(position).getItems()
						.get(i).getName());
			}

			return convertView;
		}

		class ViewHouder {
			TextView name;
		}

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		if (key_word.getText().toString().length() > 0) {
			search.setVisibility(View.VISIBLE);
			searchCancel.setVisibility(View.GONE);
		} else {
			search.setVisibility(View.GONE);
			searchCancel.setVisibility(View.VISIBLE);
		}
	}

}
